package com.ychug.gateway.system;



import com.ychug.common.base.ResultAjaxHash;
import com.ychug.framework.web.base.BaseController;
import com.ychug.framework.web.page.TableDataInfo;
import com.ychug.service.system.entity.SysDictTypeEntity;
import com.ychug.service.system.service.ISysDictTypeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 数据字典信息
 * 
 * @author dds
 */
@Controller
@RequestMapping("/sys/dict")
public class SysDictTypeController extends BaseController
{
    private String prefix = "modules/sys/dict/type";

    @Autowired
    private ISysDictTypeService dictTypeService;


    @GetMapping()
    public String dictType()
    {
        return prefix + "/viewType";
    }

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysDictTypeEntity dictType)
    {
        startPage();
        List<SysDictTypeEntity> list = dictTypeService.selectDictTypeList(dictType);
        return getDataTable(list);
    }


    @PostMapping("/export")
    @ResponseBody
    public ResultAjaxHash export(SysDictTypeEntity dictType)
    {

//        List<SysDictTypeEntity> list = dictTypeService.selectDictTypeList(dictType);
//        ExcelUtil<SysDictTypeEntity> utils = new ExcelUtil<SysDictType>(SysDictType.class);
//        return utils.exportExcel(list, "dictType");
        return null;
    }

    /**
     * 新增字典类型
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/addType";
    }

    /**
     * 新增保存字典类型
     */

    @PostMapping("/add")
    @ResponseBody
    public ResultAjaxHash addSave(SysDictTypeEntity dict)
    {
        dict.setCreateBy("admin");
        return toAjax(dictTypeService.insertDictType(dict));
    }

    /**
     * 修改字典类型
     */
    @GetMapping("/edit/{dictId}")
    public String edit(@PathVariable("dictId") Long dictId, ModelMap mmap)
    {
        mmap.put("dict", dictTypeService.selectDictTypeById(dictId));
        return prefix + "/editType";
    }

    /**
     * 修改保存字典类型
     */

    @PostMapping("/edit")
    @ResponseBody
    public ResultAjaxHash editSave(SysDictTypeEntity dict)
    {
        dict.setUpdateBy("admin");
        return toAjax(dictTypeService.updateDictType(dict));
    }


    @PostMapping("/remove")
    @ResponseBody
    public ResultAjaxHash remove(String ids)
    {
        try
        {
            return toAjax(dictTypeService.deleteDictTypeByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 查询字典详细
     */

    @GetMapping("/detail/{dictId}")
    public String detail(@PathVariable("dictId") Long dictId, ModelMap mmap)
    {
        mmap.put("dict", dictTypeService.selectDictTypeById(dictId));
        mmap.put("dictList", dictTypeService.selectDictTypeAll());
        return "sys/dict/data";
    }

    /**
     * 校验字典类型
     */
    @PostMapping("/checkDictTypeUnique")
    @ResponseBody
    public String checkDictTypeUnique(SysDictTypeEntity dictType)
    {
        return dictTypeService.checkDictTypeUnique(dictType);
    }



}
